package com.spelet.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3;
import com.spelet.GamePreferences;
import com.spelet.Main;
import com.spelet.Renderer;
import com.spelet.StaticSprites;

public class MainMenu extends Screen {
	Main main;
	public OrthographicCamera cam;
	Vector3 translationVector;
	int activeBG=0;
	int activeC=0;
	int timer;
	int cloud1;
	int cloud2;
	int cloud3;
	private Sprite hatman;
	public MainMenu(Main m) {
		cam = new OrthographicCamera();
		translationVector = new Vector3();
		hatman = StaticSprites.mainhatman;
		main = m;
	}

	public void enter() {
		Gdx.input.setCatchBackKey(true);
		Gdx.input.setInputProcessor(this);
	}

	@Override
	public boolean keyDown(int arg0) {
		if (Input.Keys.BACK == arg0) {
			main.setScreen(Main.SCREEN_MAINMENU);
		}
		return false;
	}

	@Override
	public boolean keyTyped(char arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyUp(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean scrolled(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int x, int y, int arg2, int arg3) {
		translationVector.set(x,y,0);
		cam.unproject(translationVector);
		if(StaticSprites.mainmenuplay.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			int getLevel = 0;
			for(int i = 0; i < GamePreferences.levels; i++){
				if(GamePreferences.unlocked[i])
					getLevel++;
			}
			((Game) main.getState(Main.SCREEN_GAME)).setLevel(getLevel);
			main.setScreen(Main.SCREEN_GAME);
		}
		if(StaticSprites.mainmenulevelselect.getBoundingRectangle().contains(translationVector.x, translationVector.y)){
			main.setScreen(Main.SCREEN_LEVELSELECT);
		} else if (StaticSprites.mainmenuleveledit.getBoundingRectangle().contains(translationVector.x,translationVector.y)){
			main.setScreen(Main.SCREEN_MAPEDITOR);
		} else if (StaticSprites.mainmenuexit.getBoundingRectangle().contains(translationVector.x,translationVector.y)){
			System.exit(1);
		} else if (StaticSprites.mainmenucustomlevels.getBoundingRectangle().contains(translationVector.x,translationVector.y)){
			main.setScreen(Main.SCREEN_LOADFILE);
		}
		return false;
	}

	@Override
	public boolean touchDragged(int arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchMoved(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void update() {
		timer += Gdx.graphics.getDeltaTime()*1000;
		cloud1 += Gdx.graphics.getDeltaTime()*1000;
		cloud2 += Gdx.graphics.getDeltaTime()*1000;
		cloud3 += Gdx.graphics.getDeltaTime()*1000;
		int step = 250;
		if (timer > step*6)
			timer = 0;
		for (int i = 0; i < 6; i++) {
			if (timer < step*(i+1)) {
				activeBG = i;
				break;
			}
		}
		if (StaticSprites.mainmenuclouds[0].getX() < (Gdx.graphics.getWidth()/2)*cam.zoom) {
			StaticSprites.mainmenuclouds[0].setX(StaticSprites.mainmenuclouds[0].getX()+5);
			StaticSprites.mainmenuclouds[0].setY(Gdx.graphics.getHeight()*cam.zoom-StaticSprites.mainmenuclouds[0].getHeight());
		} else if (StaticSprites.mainmenuclouds[0].getY() > (Gdx.graphics.getHeight()/2)*cam.zoom+100) {
			StaticSprites.mainmenuclouds[0].setY(StaticSprites.mainmenuclouds[0].getY()-5);
		} else if (StaticSprites.mainmenuclouds[0].getX() < (Gdx.graphics.getWidth()+StaticSprites.mainmenuclouds[0].getWidth())*cam.zoom) {
			StaticSprites.mainmenuclouds[0].setX(StaticSprites.mainmenuclouds[0].getX()+5);
		} else
			StaticSprites.mainmenuclouds[0].setX(-StaticSprites.mainmenuclouds[0].getWidth()-50);

		StaticSprites.mainmenuclouds[1].setY(Gdx.graphics.getHeight()*cam.zoom-StaticSprites.mainmenuclouds[1].getHeight()*2);
		if (StaticSprites.mainmenuclouds[1].getX() < Gdx.graphics.getWidth()*cam.zoom+StaticSprites.mainmenuclouds[1].getWidth())
			StaticSprites.mainmenuclouds[1].setX(StaticSprites.mainmenuclouds[1].getX()+4);
		else
			StaticSprites.mainmenuclouds[1].setX(-StaticSprites.mainmenuclouds[1].getWidth()*cam.zoom);
		
		StaticSprites.mainmenuclouds[2].setY(Gdx.graphics.getHeight()*cam.zoom-StaticSprites.mainmenuclouds[2].getHeight()*1.5f);
		if (StaticSprites.mainmenuclouds[2].getX() < (Gdx.graphics.getWidth()*cam.zoom+StaticSprites.mainmenuclouds[2].getWidth()))
			StaticSprites.mainmenuclouds[2].setX(StaticSprites.mainmenuclouds[2].getX()+3);
		else
			StaticSprites.mainmenuclouds[2].setX(-StaticSprites.mainmenuclouds[2].getWidth());
		
		if(MathUtils.random(1000) < 5){
			changeEyes();
		}
	}
	
	public void changeEyes(){
		if(hatman == StaticSprites.mainhatman){
			hatman = StaticSprites.mainhatman2;
		}
		else{
			hatman = StaticSprites.mainhatman;
		}
	}

	@Override
	public void render() {
		Renderer.getInstance().renderMainMenu(this);
	}
	public void renderMainMenu(SpriteBatch sb) {
		StaticSprites.mainskybg.draw(sb);
		StaticSprites.mainmenuclouds[0].draw(sb);
		StaticSprites.mainmenuclouds[1].draw(sb);
		StaticSprites.mainmenuclouds[2].draw(sb);
		StaticSprites.mainbg[activeBG].setPosition(cam.position.x-StaticSprites.mainbg[activeBG].getWidth()/2, 0);
		StaticSprites.mainbg[activeBG].draw(sb);

		hatman.setPosition(cam.position.x-hatman.getWidth()/2, cam.position.y-hatman.getHeight()/2);
		hatman.draw(sb);
		StaticSprites.title.setPosition(cam.position.x-StaticSprites.title.getWidth()/2, 700);
		StaticSprites.title.draw(sb);
		StaticSprites.mainmenuplay.setPosition(cam.position.x-StaticSprites.mainmenuplay.getWidth(),520);
		StaticSprites.mainmenuplay.draw(sb);
		StaticSprites.mainmenulevelselect.setPosition(cam.position.x-StaticSprites.mainmenulevelselect.getWidth(),440);
		StaticSprites.mainmenulevelselect.draw(sb);
		StaticSprites.mainmenucustomlevels.setPosition(cam.position.x-StaticSprites.mainmenuexit.getWidth(),360);
		StaticSprites.mainmenucustomlevels.draw(sb);
		StaticSprites.mainmenuleveledit.setPosition(cam.position.x-StaticSprites.mainmenuleveledit.getWidth(),280);
		StaticSprites.mainmenuleveledit.draw(sb);
		StaticSprites.mainmenuexit.setPosition(cam.position.x-StaticSprites.mainmenuexit.getWidth(),60);
		StaticSprites.mainmenuexit.draw(sb);
	}
}
